<%-- 
    Document   : engine
    Created on : Jul 4, 2011, 3:43:08 PM
    Author     : omar
--%>

<%@page import="java.util.HashMap"%>
<%@page import="java.lang.reflect.Method"%>
<%@page import="Engine.Problem"%>
<%@page import="java.util.ArrayList"%>
<%@page import="Engine.Submission"%>
<%@page import="Engine.Timus"%>
<%@page import="Engine.PKU"%>
<%@page import="Engine.LiveArchive"%>
<%@page import="Engine.CodeForces"%>
<%@page import="Engine.UVA"%>
<%@page import="Engine.Judge"%>
<%@page import="Engine.SPOJ"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            out.print("O_O");
            String id = request.getParameter("ID");
            String jid = request.getParameter("JID");
            Judge j = (Judge)Class.forName("Engine."+jid).newInstance();
            // Sign In
            if (id.equals("1")) {
                String name = request.getParameter("username");
                HashMap<String , Integer> ref = (HashMap<String , Integer>)j.getClass().getField("signedInUsers").get(j);
                if(!ref.containsKey(name)){
                    Class[] args = new Class[] { String.class, String.class, String.class, String.class, String.class, Long.class, int.class};
                    j.getClass().getConstructor(args).newInstance(name , request.getParameter("password"), null, null, null,null , 1);
                    ref.put(name , -1);
                    out.print("|-1|");
                }
                else if(ref.get(name).equals(-1))
                    out.print("|-1|");
                else {
                    out.print("|" + ref.get(name) + "|");
                    ref.remove(name);
                }             
            } // Sign Out
            else if (id.equals("2")) {
                out.print("|" + j.signOut(request.getParameter("username")) + "|");
            } // Submit Problem
            else if (id.equals("3")) {
                j.getClass().getField("submissionLocalId").set(j, Long.parseLong(j.getClass().getField("submissionLocalId").get(j).toString())+1L);
                Long idl = Long.parseLong(j.getClass().getField("submissionLocalId").get(j).toString());
                Class[] args = new Class[] { String.class, String.class, String.class, String.class, String.class, Long.class, int.class};
                j.getClass().getConstructor(args).newInstance(request.getParameter("username"), request.getParameter("password"), request.getParameter("problemId"), request.getParameter("languageId"), request.getParameter("code"),idl , 2);
                out.print("|" + idl + "|");
            } // Get Last Submission
            else if (id.equals("4")) {
             Submission s = new Submission("", "", "", "", "", "");
             HashMap<Long , String> idsMap =(HashMap<Long, String>) j.getClass().getField("idsMap").get(j);
             HashMap<String , Submission> submissions =(HashMap<String, Submission>) j.getClass().getField("submissions").get(j); 
             String key = idsMap.get(Long.parseLong(request.getParameter("idl")));
             if(submissions.containsKey(key))
             {
                s = submissions.get(key);
                submissions.remove(key);
             }
             out.print("|" + s.getDate() + "|" + s.getLanguage() + "|" + s.getMemoryUsed() + "|" + s.getProblemId() + "|" + s.getRuntime() + "|" + s.getStatus() + "|");
            } // Get All Problems
            else if (id.equals("5")) {
                ArrayList<Problem> a = j.getAllProblems();
                for (int k = 0; k < a.size(); k++) {
                    Method[] m = a.get(k).getClass().getDeclaredMethods();
                    for (int i = 0; i < m.length; i++) {
                        if (m[i].toGenericString().indexOf("get") != -1) {
                            out.print(m[i].invoke(a.get(k)) + " | ");
                        }
                    }
                    out.print("\n");
                }

            } // Get Problems Solved
            else if (id.equals("6")) {
                ArrayList<String> a = j.getProblemsSolved(request.getParameter("coderId"));
                for (int i = 0; i < a.size(); i++) {
                    out.print("|" + a.get(i));
                }
            } // Get Problem Info
            else if (id.equals("7")) {
                Problem p = j.getProblemInfo(request.getParameter("problemId"));
                Method[] m = p.getClass().getDeclaredMethods();
                for (int i = 0; i < m.length; i++) {
                    if (m[i].toGenericString().indexOf("get") != -1) {
                        out.print("|" + m[i].invoke(p));
                    }
                }

            }
            out.print("O_O");
        %>
    </body>
</html>
